package com.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bean.LoginBean;
import com.bean.WorkerBean;
import com.dao.LoginDAO;

import DBAccess.DBAccess;

/**
 * Servlet implementation class Controller
 */
public class Controller extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Controller() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		if (request.getParameter("whereFrom").equals("login")) {
			HttpSession session = request.getSession();
			LoginBean lgb = new LoginBean(request.getParameter("userID"),
					request.getParameter("password"),
					request.getParameter("newPassword"),
					request.getParameter("confirmNewPassword"),
					request.getParameter("role"));
			String requestStatus = LoginDAO.loginGateway(lgb);

			if (requestStatus != null) {
				response.getWriter().println("Login Success !");
				// Setting Session Attributes UserID and Role;
				session.setAttribute("user", request.getParameter("userID"));
				session.setAttribute("userRole", requestStatus);
				request.getRequestDispatcher("/JSP/welcome.jsp").forward(
						request, response);

			} else {
				response.getWriter().println("Invalid Login !");
			}

			// String
			// query="Select * from Login where userid="+request.getParameter("userID")+"AND password="+request.getParameter("password")+";";

		}
		if (request.getParameter("whereFrom").equals("addWorker.jsp")) {

			response.getWriter().println(request.getParameter("name"));
			response.getWriter().println(request.getParameter("jobTitle"));
			response.getWriter().println(request.getParameter("address"));
			response.getWriter().println(request.getParameter("phone"));
			response.getWriter().println(request.getParameter("hireDate"));

			// create table if not exists employee
			// (
			// name varchar(60),
			// jobTitle varchar(60),
			// address varchar(200),
			// towards varchar(100),
			// phone varchar(20),
			// hireDate date
			// );

			String query = "INSERT INTO Employee Values ('"
					+ request.getParameter("name") + "','"
					+ request.getParameter("jobTitle") + "','"
					+ request.getParameter("address") + "','"
					+ request.getParameter("phone") + "','"
					+ request.getParameter("hireDate") + "',NEXT VALUE FOR employee_id)";
			response.getWriter().println(query);

			try {
				// DBAccess.update("drop table Employee");
				// DBAccess.update("create table if not exists employee(name varchar(60),jobTitle varchar(60),address varchar(200),phone varchar(20),hireDate date)");
				DBAccess.update(query);
				response.getWriter().println("Value Inserted ! ");
				ArrayList<WorkerBean> WorkerList = DBAccess
						.query("SELECT * FROM EMPLOYEE");
				request.setAttribute("WorkerList", WorkerList);
				
				
				request.setAttribute("whereTo", "viewWorker.jsp");
				request.getRequestDispatcher("JSP/welcome.jsp").forward(
						request, response);
				// DBAccess.query("Select * from CommonExpense");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
